package com.qianbei.user;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.qianbei.R;
import com.qianbei.common.base.ActivityManagers;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.SetData;
import com.qianbei.common.init.SystemConfig;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.net.view.ProgressDialog;
import com.qianbei.common.rxbus.RxBus;
import com.qianbei.common.util.ConstantValue;
import com.qianbei.common.view.IosLikeToggleButton;
import com.qianbei.user.privacy.AboutActivity;
import com.qianbei.user.unlogin.UserLoginOut;


public class SetActivity extends BaseActivity  {

    private IosLikeToggleButton soundSwith, vibrationSwith;
    private TextView mExitTv;
    private TextView mShowPhoneTv;

    private String link;
    private int mRequireUpdate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_set_activity);
        setTitle("设置");

    }

    @Override
    public void initView() {
        super.initView();
        soundSwith = (IosLikeToggleButton) findViewById(R.id.set_text_sound);
        vibrationSwith = (IosLikeToggleButton) findViewById(R.id.set_text_vibration);
        mExitTv = (TextView) findViewById(R.id.user_unlogin);
        mShowPhoneTv = (TextView) findViewById(R.id.tv_bind_phone);
        findViewById(R.id.check_version).setOnClickListener(this);
        findViewById(R.id.modify_phone_layout).setOnClickListener(this);
        findViewById(R.id.about_us).setOnClickListener(this);
        mExitTv.setOnClickListener(this);

        mShowPhoneTv.setText(SetData.getUserPhone());
        soundSwith.setChecked(SetData.getSound());
        vibrationSwith.setChecked(SetData.getVibrate());
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.check_version:
                requestNeedUP();
                break;
            case R.id.user_unlogin:
                if (!TextUtils.isEmpty(SetData.getUserID())) {
                    outLgin();
                }
                break;
            case R.id.modify_phone_layout:
                Intent intent = new Intent(this, BindPhoneActivity.class);
                intent.putExtra(ConstantValue.SHOW_PHONE_KEY, true);
                startActivityForResult(intent, 1);
                break;

            case R.id.about_us:
                jump(AboutActivity.class);
                break;
            default:
                break;
        }
    }


    public void requestNeedUP() {
        ServerControl sr = new ServerControl(Method.GET, CHECK_VERSION + SystemConfig.getInstance().version + "?");
        sr.serverListener = this;
        new ProgressDialog(this).startControl(sr);
    }

    @SuppressLint("NewApi")
    @Override
    public void serverFinish(ServerResult sr) {

        if (sr.isContinue) {
//            boolean update = sr.bodyData.optBoolean("update");
//
//            String message = sr.bodyData.optString("update_log");
            link = sr.bodyData.optJSONObject("data").optString("address");
//
//            if (update) {
            mRequireUpdate = sr.bodyData.optJSONObject("data").optInt("require_flag", 0); //是否强制更新
//                String title = sr.bodyData.optString("title");
//
//
//                if (TextUtils.isEmpty(title)) {
//                    title = "更新提示";
//                }

//            } else {
//                new MsgAlert().show("已经是最新版本");
//            }
        }else {
            new MsgAlert().show("您已经是最新版本！");
        }
    }



    /**
     * 退出登录
     */
    public void outLgin() {
        ServerControl sc = new ServerControl(Method.GET, EXIT_LOGIN
                + SetData.getUserID() + "?");

        sc.serverListener = new ServerControl.ServerListener() {

            @Override
            public void serverFinish(ServerResult sr) {


            }
        };
        sc.startVolley();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            mShowPhoneTv.setText(SetData.getUserPhone());
        }
    }
}
